﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace WindowsGameAula02
{
    class Player:AnimatedObject2D
    {

        public float Speed;
        //ShotManager sm;

        public Player(Texture2D player, Texture2D shot, Vector2 position, Animation[] animations)
            : base(player, position, animations)
        {
            //sm = new ShotManager(shot);
            //sm.ShotLifeTime = 3 * 1000;
            //sm.timeBetweenShots = 250f;
            
        }
        public override void Update(GameTime gameTime){
        

            if (Input.GetKey(Keys.W))
            {
                Speed+= 0.1f;
                if (Origin != new Vector2(Origin.X, 0))
                {
                    Move(new Vector2((float)Math.Sin(Rotation), -(float)Math.Cos(Rotation)), Rect.Height);
                    Origin = new Vector2(Origin.X, 0);
                }
            }
            else if (Input.GetKey(Keys.S))
            {
                Speed -= 0.1f;
                if (Origin != new Vector2(Origin.X, Rect.Height))
                {
                    Move(new Vector2((float)Math.Sin(Rotation), -(float)Math.Cos(Rotation)), -Rect.Height);
                    Origin = new Vector2(Origin.X, Rect.Height);
                }
            }
            else
            {
                if (Speed > 0) Speed -= 0.1f;
                else if (Speed < 0) Speed += 0.1f;
                if(Math.Abs(Speed) <= 0.1f) Speed = 0;
            }
            Move(new Vector2((float)Math.Sin(Rotation), -(float)Math.Cos(Rotation)), Speed);
           
            if (Input.GetKey(Keys.A))
            {
                Rotate(-0.1f);
            }
            else if (Input.GetKey(Keys.D))
            {
                Rotate(0.1f);
            }
            //if (Input.GetKey(Keys.Space))
            //{
            //    sm.NewShot(gameTime, Position,
            //        new Vector2((float)Math.Sin(Rotation), -(float)Math.Cos(Rotation)), Math.Abs(Speed) * 2 + 1f);
            //}
            //sm.Update(gameTime);
            base.Update(gameTime);
        }

        public void Move(Vector2 dir, float speed)
        {
            if (speed == 0) return;
            this.Position += dir * speed;
        }
        public void Rotate(float amount)
        {
            this.Rotation += amount;
        }
        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {
            //sm.Draw(spriteBatch, gameTime);
            base.Draw(spriteBatch, gameTime);
        }
    }
}
